#!/bin/bash
# demo5: Run the commands from the Lopper openamp demo

MY_DIR=$(dirname $0)

WAIT=true

case $1 in
--no-wait)
    shift
    WAIT=false
    ;;
*)
    ;;
esac

step-run() {
    STEP="$1"; shift
    COMMENT="$1"; shift
    echo "*** $STEP ***"
    if [ -n "$COMMENT" ]; then
        echo -e "$COMMENT"
    fi
    if $WAIT; then
        echo "about to run this command:"
        echo "$@"
        echo "hit enter to run it, ^c to stop"
        read
    fi
    "$@"
    if [ $? -ne 0 ]; then
        echo "Command returned non zero status \$?=$?"
    fi
}

step-view() {
    STEP="$1"; shift
    COMMENT="$1"; shift
    echo "*** $STEP ***"
    if [ -n "$COMMENT" ]; then
        echo -e "$COMMENT"
    fi
    if $WAIT; then
        echo "about to look at this file:"
        echo "$@"
        echo "hit enter to view it, ^c to stop"
        echo "while viewing hit q to exit (you are using less)"
        read
        "$@" | less
    else
        "$@"
    fi
}

step-comment() {
    STEP="$1"; shift
    echo "*** $STEP ***"
    echo -e "$@"
    if $WAIT; then
        echo "hit enter to continue, ^c to stop"
        read
    fi
}

. $MY_DIR/.venv/bin/activate
cd $MY_DIR/lopper/demos/openamp
mkdir -p scratch
export LOPPER_DIR=$(cd ../..; pwd)

step-comment "README" \
    "Bring up the following README file in a browser to follow along:\n" \
    "   https://github.com/devicetree-org/lopper/blob/systemdt-linaro-demo/demos/openamp/README.md"

step-comment "step 1 & 2" \
    "Step 1 has already been done for you in the container setup\n" \
    "   demo5-setup was used to do this, you can look at this later" \
    "\nStep 2 is also done as we have already switched to lopper/demos/openamp"

step-run "step 3" "" \
lopper -f -O scratch --enhanced --permissive \
    -a openamp.py -a openamp_xlnx.py -a openamp-xlnx-zynq.py \
    -i ./inputs/openamp-overlay-zynqmp.yaml \
    -i $LOPPER_DIR/lopper/lops/lop-xlate-yaml.dts \
    -i $LOPPER_DIR/lopper/lops/lop-a53-imux.dts \
    -i $LOPPER_DIR/lopper/lops/lop-domain-linux-a53.dts \
    -i $LOPPER_DIR/lopper/lops/lop-openamp-versal.dts \
    -i $LOPPER_DIR/lopper/lops/lop-domain-linux-a53-prune.dts \
    inputs/system-dt/system-top.dts linux-boot.dts

step-view "step 3a" "" \
cat linux-boot.dts

step-view "step 3b" "" \
cat openamp-channel-info.txt

step-view "step 3c" "" \
diff -u inputs/openamp-overlay-zynqmp.yaml inputs/openamp-overlay-zynqmp-dev-mem.yaml

step-run "step 3d" "" \
lopper -f -O scratch --enhanced --permissive \
    -a openamp.py -a openamp_xlnx.py -a openamp-xlnx-zynq.py \
    -i ./inputs/openamp-overlay-zynqmp-dev-mem.yaml \
    -i $LOPPER_DIR/lopper/lops/lop-xlate-yaml.dts \
    -i $LOPPER_DIR/lopper/lops/lop-a53-imux.dts \
    -i $LOPPER_DIR/lopper/lops/lop-domain-linux-a53.dts \
    -i $LOPPER_DIR/lopper/lops/lop-openamp-versal.dts \
    -i $LOPPER_DIR/lopper/lops/lop-domain-linux-a53-prune.dts \
    inputs/system-dt/system-top.dts linux-boot2.dts

step-view "step 3d output" "" \
diff -u linux-boot.dts linux-boot2.dts

step-run "step 4" "Now we start the Xen SysDT demo" \
lopper --permissive -f inputs/dt/host-device-tree.dts system-device-tree-out.dts  -- \
      extract -t /axi/serial@ff010000 \
      -i zynqmp-firmware \
      -x pinctrl-0 -x pinctrl-names -x power-domains -x current-speed \
      -x resets -x 'interrupt-controller.*' -- \
      extract-xen -t serial@ff010000 -o serial@ff010000.dts

step-view "step 4a" "" \
cat serial@ff010000.dts

step-view "step 4b" "" \
grep -A2 -B16 xen,passthrough system-device-tree-out.dts

step-run "step 4c" "" \
lopper --permissive -f system-device-tree-out.dts system-device-tree-out-final.dts  -- \
    extract -o extracted_tree.dts -p -t ethernet@ff0e0000 \
    -i zynqmp-firmware \
    -x 'interrupt-controller.*' -x power-domains -x current-speed -- \
    extract-xen -v -t ethernet@ff0e0000 -o xen-passthrough-eth.dts

step-view "step 4c output" "" \
grep -A2 -B24 xen,passthrough system-device-tree-out-final.dts

step-comment "Done" "The files are all in $PWD"
